class BrowsedRecord < ActiveRecord::Base

  attr_protected []

  belongs_to :project
  belongs_to :building
  belongs_to :customer
  belongs_to :user
  belongs_to :enterprise 

  validates :user_id,
    :enterprise_id,
    :presence => true

  default_scope lambda { 
    where( :enterprise_id => Enterprise.current_id, :user_id => User.current_id ).order('browsed_at desc')
  }

  before_create do |browsed_record|
    browsed_record.browsed_at = DateTime.now
    browsed_record.user = User.current
  end
  
  def self.browses_it!(options = {})
      defaults_options = { :user_id => User.current_id }.merge(options)
      where(defaults_options).any? ? where(defaults_options).first.touch(:browsed_at) : create(defaults_options)
  end
end
